<!--
 * @Author: wankong
 * @Desc: 
 * @Date: 2021-10-10 20:13:43
 * @LastEditTime: 2021-10-18 22:02:57
-->
<template>
  <div class="setting">
    <el-row>
      <el-col>
        <el-button @click="showDialog = true">添加一项</el-button>
      </el-col>
    </el-row>
    <el-form label-width="150px">
      <el-form-item
        :label="option.title"
        v-for="option in settings"
        :key="option.id"
      >
        <el-input v-model="option.ovalue"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="changeList()"> 修改</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="showDialog" width="40%" title="新增设置项">
      <el-form
        :model="option"
        ref="setAddRef"
        :rules="setAddRule"
        label-width="100px"
      >
        <el-form-item label="关键字:" prop="name">
          <el-input v-model="option.name"></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="title">
          <el-input v-model="option.title"></el-input>
        </el-form-item>
        <el-form-item label="值:" prop="ovalue">
          <el-input v-model="option.ovalue"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">重置</el-button>
        <el-button type="primary" @click="submitAdd()">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      settings: null,
      showDialog: false,
      option: {
        name: "",
        title: "",
        ovalue: "",
      },
      setAddRule: {
        name: {
          required: true,
          message: "关键字要求必填",
          trigger: "blur",
        },
        title: {
          required: true,
          message: "描述信息必填",
          trigger: "blur",
        },
        ovalue: {
          required: true,
          message: "内容还没填呢！！！",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    async getSettingOptions() {
      const res = await this.$http.get(`${this.adminPre}/setting`);
      this.settings = res.data;
    },
    async changeList() {
      const res = await this.$http.post(
        `${this.adminPre}/setting`,
        this.settings
      );
      if (res.code == 200) {
        this.$message.success("修改成功！");

        this.getSettingOptions();
      } else {
        this.$message.error("修改失败！");
      }
    },
    submitAdd() {
      this.$refs["setAddRef"].validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.post(
          `${this.adminPre}/setting/add`,
          this.option
        );
        if (res.code == 200) {
          this.$message.success("添加成功！");

          this.getSettingOptions();
        } else {
          this.$message.error("添加失败！");
        }
      });
    },
  },
  created() {
    this.getSettingOptions();
  },
};
</script>

<style>
</style>